<script>
	import {
		mapMutations
	} from "vuex";
	import store from "@/store/index.js";
	export default {

		onLaunch: function() {
			const that = this
			const sinfo = uni.getSystemInfoSync();
			//企业微信
			if (sinfo.environment && sinfo.environment == 'wxwork') {
				wx.qy.login({
					success: function(res) {
						that.getQYWXEmpower(res.code)
					}
				});
			} else {

				let userInfo = uni.getStorageSync('userInfo')
				if (userInfo && userInfo.token && userInfo.existFlag == 1) {} else {
					uni.reLaunch({
						url: '/pages/subPackagesD/login/index'
					})
				}
			}
			const updateManager = uni.getUpdateManager()
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
				console.log(res.hasUpdate)
			})
			updateManager.onUpdateReady(function() {
				uni.showModal({
					title: '更新提示',
					showCancel: false,
					content: '新版本已经准备好，是否重启应用？',
					success: function(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate()
						}
					}
				})
			})
			updateManager.onUpdateFailed(function() {

			})
		},
		onShow: function() {
			uni.hideTabBar({
				animation: false
			})
		},
		onHide: function() {

		},
		methods: {
			...mapMutations(["setRoleId", "setfirstLoginFlag", "setmenuStatus"]),
			getQYWXEmpower(code) {
				const that = this
				let params = {
					code
				}
				uni.$u.http.post(`/base/mobile/userInfo/qywx/login`, params)
					.then(res => {
						if (res.code == 0) {
							this.setRoleId(0);
							that.resultInfo = res.data
							let existFlag = res.data.existFlag;
							let token = res.data.token;
							//由于这里逻辑特殊 后续登录会替换掉状态值 所以单独保存一份状态值来做判断
							uni.setStorageSync('userInfo', res.data);
							//绑定id
							uni.setStorageSync('thirdId', res.data.thirdId);
							uni.setStorageSync('bindType', 2);
							this.setfirstLoginFlag(res.data.firstLoginFlag)
							if (token) {
								uni.$u.http.get('/base/mobile/sysMenuInfo/getMobileMenuListByRole').then(res => {
									if (res.code == 0) {
										uni.setStorageSync('menu', res.data);
										this.setmenuStatus(1)
									}
								}).catch(error => {
									uni.$u.toast(error)
								})
							}
						} else if (res.code == 10030) {
							uni.navigateTo({
								url: '/pages/subPackagesD/expire/index'
							})
						} else if (res.code == 10012) {
							uni.removeStorageSync('userInfo');
							uni.navigateTo({
								url: '/pages/subPackagesD/noAuthority/index'
							})

						}
					}).catch(err => {
						uni.$u.toast(err)
					})
			},
			

		}


	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "common/demo.scss";
	@import "common/iconfont.css";
</style>
